package stdio.costa.imoca.api;

import javax.mail.internet.AddressException;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import stdio.costa.imoca.converter.MocaUser2MocaAccessToken;
import stdio.costa.imoca.model.MocaAccessToken;
import stdio.costa.imoca.model.MocaUser;
import stdio.costa.imoca.spi.MocaUserSpi;
import stdio.costa.imoca.spi.SendForgetPasswordEmailSpi;
import stdio.costa.imoca.utils.Constants;

@Path("/mocauser")
@Component
@Scope("request")
public class MocaUserApi extends BaseApi {

	@Autowired
	private MocaUserSpi mocaUserSpi;
	
	@Autowired
	private SendForgetPasswordEmailSpi sendForgetPasswordEmailSpi;

	@Path("/signup")
	@POST
	@Produces({ "application/json", "application/xml" })
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Transactional
	public MocaAccessToken signup(@QueryParam("name") String name,
			@QueryParam("password") String password,
			@QueryParam("email") String email,
			@QueryParam("phone") String phone,
			@QueryParam("signupType") String signupType,
			@QueryParam("realname") String realname) {

		MocaUser mocaUser = mocaUserSpi.findUserByNameOrEmailOrPhone(name,
				email, phone);

		// should add validation
		mocaUser = new MocaUser();

		mocaUser.setName(name);
		mocaUser.setPassword(password);
		mocaUser.setEmail(email);
		mocaUser.setPhone(phone);

		mocaUser.setPassword(DigestUtils.md5Hex(mocaUser.getPassword()));
		mocaUserSpi.save(mocaUser);

		MocaAccessToken mocaAccessToken = MocaUser2MocaAccessToken
				.convert(mocaUser);

		return mocaAccessToken;

	}

	@Path("/login")
	@GET
	@Produces({ "application/json" })
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Transactional
	public MocaAccessToken login(@QueryParam("login_key") String loginname,
			@QueryParam("password") String password,
			@QueryParam("login_type") String loginType) {

		MocaUser mocaUser = new MocaUser();

		if (Constants.SIGNUP_BY_USERNAME.equals(loginType)) {
			mocaUser = mocaUserSpi.loginByUsername(loginname, password);
		} else if (Constants.SIGNUP_BY_EMAIL.equals(loginType)) {
			mocaUser = mocaUserSpi.loginByEmail(loginname, password);
		} else if (Constants.SIGNUP_BY_PHONE.equals(loginType)) {
			mocaUser = mocaUserSpi.loginByPhone(loginname, password);
		} else {
			mocaUser = null;
		}

		MocaAccessToken mocaAccessToken = MocaUser2MocaAccessToken
				.convert(mocaUser);

		return mocaAccessToken;
	}

	@Path("/forget_password")
	@POST
	@Produces({ "application/json" })
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Transactional
	public void sendforgetPasswordEmail(
			@QueryParam("login_key") String loginKey) throws AddressException {

		MocaUser mocaUser  = mocaUserSpi.findUserByNameOrEmailOrPhone(loginKey, loginKey, loginKey);

		if(mocaUser==null){
			
		}
		
		sendForgetPasswordEmailSpi.sendEmail(mocaUser);

	}
}
